package j01die;

public class J3jiejue {
    static Object lock1 = new Object();
    static Object lock2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            synchronized (lock1){
                System.out.println("t1 - enter lock1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock1){
                    try {
                        //程序暂停，开锁lock1
                        lock1.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                synchronized (lock2){
                    System.out.println("t1 - enter lock2");
                    System.out.println("t1 - end lock2");
                }
                System.out.println("t1 - end lock1");
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (lock2){
                System.out.println("t2222 - enter lock2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                synchronized (lock1){
                    System.out.println("t2222 - enter lock1");
                    System.out.println("t2222 - end lock1");
                }
                System.out.println("t2222 - end lock2");
            }
            synchronized (lock1){
                //通知lock1启动
                lock1.notify();
            }

        });
        t1.start();
        t2.start();
    }
}
